var datatable;// 定义一个全局的dataTable
var s1mme;
var s11;
var s2a;
var gxgy;
var s5s8;
var sta;
var s6a;
var interfaceType;
$(function() {
	
	initInfo()
	
	$('#tableIM tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
})

function queryAll(){
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    var userValue=$("#userValue").val();  
	if(startTime==null || startTime==""){
		layer.alert("开始时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(endTime==null || endTime==""){
		layer.alert("结束时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(!checkInterval()){
		layer.alert("时间相隔不能大于1天！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(userValue==null || userValue==""){
		layer.alert("手机号码,IMSI,IMEI或者MSIP不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	getIMList();
}


function getIMList() {
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var userType=$("#userType").val();
	var userValue=$("#userValue").val();
	var interface1 = $("#interface").val();
	var status = $("#status").val();
	
	$("#tableIM tbody tr").remove();
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#tableIM').DataTable({
		"aoColumnDefs" : [ {
	        sDefaultContent : '',
	        aTargets : [ '_all' ]
	    } ],
		"bPaginate" : true, // 翻页功能
		"bLengthChange" : false, // 改变每页显示数据数量
		"bFilter" : false, // 过滤功能
		"bSort" : true, // 排序功能
		"bInfo" : true,// 页脚信息
		"bAutoWidth" : true,// 自动宽度
		// "sPaginationType": "full_numbers",//显示分页的样式 还有simple_numbers等
		"sScrollX" : "100%",
		"sScrollXInner": "110%",
		"bScrollCollapse" : true,
		"oLanguage" : {
			"sLengthMenu" : "每页显示 _MENU_条",
			"sZeroRecords" : "没有找到符合条件的数据",
			"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
			"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
			"sInfoEmpty" : "没有记录",
			"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
			"sSearch" : "搜索：",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "前一页",
				"sNext" : "后一页",
				"sLast" : "尾页"
			}
		},
		"bProcessing" : true,
		"bServerSide" : true,
		"sAjaxSource" : "../../kpi/userSignalDetails/getUserSignalDetailInfo",
		"fnServerData" : function(sSource, aoData, fnCallback) {
			$.ajax({
				"type" : "post",
				"contentType" : "application/x-www-form-urlencoded",
				"url" : sSource,
				"dataType" : "json",
				"data" : {
					"aoData" : JSON.stringify(aoData),
					"beginDate" : beginDate,
					"endDate" : endDate,
					"userType" : userType,
					"userValue" : userValue,
					"interface" : interface1,
					"status"   : status
				}, // 以json格式传递
				"success" : function(resp) {
					fnCallback(resp.pageDate);
					if(!resp.flag){
						alert("查询失败");
					}
				}
			});
		},
		columns : [ {
			data : "start_date"
		}, {
			data : "end_date"
		}, {
			data : "msisdn"
		}, {
			data : "imsi"
		}, {
			data : "user_ipv4"
		}, {
			data : "imei_sv"
		}, {
			data : "interface",
			render:function(val,type,row){
				//return interfaceType[val] == null ? val : interfaceType[val];
				return interfaceType[val];
			}
		},{
			data : "procedure_type",
			render:function(val,type,row){
				var data = interfaceType[row.interface];
				if(data == 'S1-MME'){
					return s1mme[val];
				}else if(data == 'S6a'){
					return s6a[val];
				}else if(data == 'S11'){
					return s11[val];
				}else if(data == 'S2a'){
					return s2a[val];
				}else if(data == 'Gx' || data == 'Gy'){
					return gxgy[val];
				}else if(data == 'STa'){
					return sta[val];
				}else if(data == 'S5/S8'){
					return s5s8[val];
				}
			}
		}, {
			data : "status",
			render:function(val,type,row){
				if(val == '0'){
					return '成功';
				}else if(val == '1'){
					return '失败';
				}
			}
		},{
			data : "status",
			render:function(val,type,row){
				var phoneNumber=row.msisdn;
				var imsi=row.imsi;
				var beginDate=row.start_date;
				var endDate=row.end_date;
				return "<a href='#' onclick=\"forwordSignalBack('"+phoneNumber+"','"+imsi+"','"+beginDate+"','"+endDate+"')\"  >查看信令</a>";
			}
		},{
			data : "terminal_model"
		}, {
			data : "apn"
		},{
			data : "ecgi" ,
			render:function(val,type,row){
				if(val == '-1' ){
					return "";
				}else{
					return val;
				}
			}
		},{
			data : "cell_name"
		}]
	});
}


function exportExcel() {
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var userType=$("#userType").val();
	var userValue=$("#userValue").val();
	var interface1 = $("#interface").val();
	var status = $("#status").val();
	
	if(beginDate==null || beginDate==""){
		layer.alert("开始时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(endDate==null || endDate==""){
		layer.alert("结束时间不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(!checkInterval()){
		layer.alert("时间相隔不能大于1天！", {
			icon : 1,
			offset: '100px'
		});
		return;
	};
	if(userValue==null || userValue==""){
		layer.alert("手机号码,IMSI,IMEI或者MSIP不能为空", {
			icon : 1,
			offset: '100px'
		});
		return;
	}
	layer.load(2);
	$.ajax({
		type : "POST",
		url : "../../kpi/userSignalDetails/exportBigDataExcel?rand="+ Math.random(),
		dataType : "json",
		"data" : {
			"beginDate" : beginDate,
			"endDate" : endDate,
			"userType" : userType,
			"userValue" : userValue,
			"interface" : interface1,
			"status"   : status
		},
		success : function(data) {
			layer.closeAll('loading');
			if (data.flag) {
				var url = "../../kpi/userSignalDetails/downFiles?path="+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		}
	});
}

function resetForm(formId){
	$('#'+formId)[0].reset(); 
}

function initInfo(){
	s1mme = {'1' : 'attach','2' : 'Sevice Request','3' : 'Extended Service Request（CSFB）', '4' : 'Paging' , '5' : 'TAU',
			 '6' : 'Detach','7' : 'PDN connectivity','8' : 'PDN disconnection', '9' : 'EPS bearer resource allocation' , '10' : 'EPS bearer resource modify',
			 '11' : 'EPS bearer context deactivation','12' : 'EPS bearer context modification','13' : 'Dedicated EPS bearer context activation', '14' : 'Path Switch' , '15' : 'S1切出',
		     '16' : 'S1切入','17' : 'UE context setup','18' : 'UE context modification', '19' : 'UE context release' , '20' : 'E-RAB release',
		     '28' : 'Identity Acquisition','29' : 'Authenticaiton','30' : 'Security Mode Command', '100' : 'PagingResult'};
	s11 = {'1' : 'Create Session' , '2' : 'Modify Bearer' , '3' : 'Delete Session','4' : 'Modify Bearer Command', '5' : 'Delete Bearer Command',
		   '6' : 'Bearer Resource Command' , '7' : 'Create Bearer ' , '8' : 'Update Bearer','9' : 'Delete Bearer', '10' : 'Release Access Bearers',
		   '11' : 'Modify Access Bearers' , '14' : 'Change Notification' , '15' : 'Downlink Data Notification','20' : 'PGW Downlink Triggering Notification', '21' : 'Identification Request',
		   '22' : 'Context Request' , '23' : 'Forward Relocation Request' , '24' : 'Forward Relocation Complete Notification','25' : 'Forward Access Context Notification', '26' : 'Relocation Cancel Request',
		   '28' : 'Suspend Notification' , '29' : 'Create Forwarding Tunnel' , '30' : 'Resume Notification','31' : 'Create Indirect Data Forwarding Tunnel', '32' : 'Delete Indirect Data Forwarding Tunnel'};
	s2a = {'1' : '初始附着' , '2' : '切换附着' , '3' : '更新' , '4' : '去附着'};
	gxgy = {'1' : 'CCR-I' , '2' : 'CCR-U' , '3' : 'CCR-T' , '4' : 'RAR'};
	s5s8 = {'2' : 'Modify Bearer' , '3' : 'Create Session' , '5' : 'Delete Session' , '6' : 'Change Notification' , '7' : 'Resume Notification',
			'8' : 'Modify Bearer Command' , '10' : 'Delete Bearer Command' , '12' : 'Bearer Resource Command' , 
			'16' : 'Create Bearer' , '17' : 'Update Bearer' , '18' : 'Delete Bearer' , '20' : 'Suspend Notification'};
	sta = {'1' : '接入鉴权和授权' , '2' : '重鉴权和重授权' , '3' : '去附着'};
	s6a = {'1' : 'Update Location' , '2' : 'Cancel Location' , '3' : 'Purge UE' , '4' : 'Insert Subscriber Data' , '5' : 'Delete Subscriber Data' , '6' : 'Authentication Information'};
	interfaceType = {'2' : 'S1-MME' , '3' : 'S6a' , '4' : 'S11' , '7' : 'S2a' , '9' : 'Gx' , '10' : 'Gy' , '8' : 'STa' , '6' : 'S5/S8'};
	
}

function forwordSignalBack(phoneNumber,imsi,beginDate,endDate){
	var url="/kpi/signalBackTrack/index?phoneNumber="+phoneNumber+"&imsi="+imsi+"&beginDate="+beginDate+"&endDate="+endDate+"";
	linkTab("信令回溯", url);
}

function checkEndTime(){  
    var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    if(end<start){  
        return false;  
    }  
    return true;  
} 

function checkInterval(){
	var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    
    var interval=end-start;
    var time=60*60*24*1000;
    if(interval>time){
    	return false;
    }
    return true;
}